package com.chebaohui.iqs.service.impl.deal;

import java.util.ArrayList;
import java.util.List;

import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.Paramter;
import com.chebaohui.iqs.entity.base.Company;
import com.chebaohui.iqs.entity.req.CarPrice;
import com.chebaohui.iqs.entity.res.CompanyCarPrice;
import com.chebaohui.iqs.service.impl.deal.bihu.BiHuPriceThread;
import com.chebaohui.iqs.service.impl.deal.libao.LiBaoPriceThread;
import com.chebaohui.iqs.service.impl.deal.test.TestPrice;
import com.chebaohui.iqs.service.impl.deal.util.CompanyUtil;

/**
 * 保险公司车辆车险算费
 * 
 * @author xielonghai
 *
 */
public class CarPriceCompanys {

	/**
	 * 车险算费
	 * 
	 * @param param
	 * @return
	 */
	public List<CompanyCarPrice> price(Paramter<CarPrice> param) throws Exception {
		List<CompanyCarPrice> result = new ArrayList<CompanyCarPrice>();

		List<PriceThread> pri = new ArrayList<>();
		for (Company c : param.getData().getCompanys()) {// 添加保险公司报价线程组
			CompanyCode code = CompanyUtil.dealCompany(c.getCompanyCode());
			if (code.isBh()) {
				BiHuPriceThread b = new BiHuPriceThread(param.getData(), c);
				b.start();
				pri.add(b);
			} else if (code.getCode().equals(CompanyCode.LIBAO.getCode())) {
				LiBaoPriceThread lb = new LiBaoPriceThread(param.getData(), c);
				lb.start();
				pri.add(lb);
			} else if (code.getCode().equals(CompanyCode.TEST.getCode())) {
				try {
					result.add(TestPrice.getPrice(param.getData(), c.getCompanyCode(), c.getModelCode()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				CompanyCarPrice ccp = new CompanyCarPrice();
				ccp.setCompanyCode(c.getCompanyCode());
				ccp.setModelCode(c.getModelCode());
				ccp.setCode(ResultCode.N03.getCode());
				ccp.setMsg(ResultCode.N03.getMsg());
				result.add(ccp);
			}
		}

		if (!pri.isEmpty() && pri.size() > 0) {
			for (PriceThread th : pri) {
				result.add(th.getResult());
			}
		}

		return result;

	}
}
